import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class XmmCircleCachedNetworkImage extends StatelessWidget {
  const XmmCircleCachedNetworkImage({
    Key? key,
    required this.imageUrl,
    this.width,
    this.height,
    this.placeholder,
    this.error,
    this.borderColor,
    this.borderWidth,
  }) : super(key: key);

  final String imageUrl;

  final double? width;

  final double? height;

  final Widget? placeholder;

  final Widget? error;

  final Color? borderColor;

  final double? borderWidth;

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      width: width,
      height: height,
      imageUrl: imageUrl,
      placeholder: _placeholderBuilder(),
      errorWidget: _errorBuilder(),
      progressIndicatorBuilder: (context, url, progress) =>
          CircularProgressIndicator(value: progress.progress),
      imageBuilder: (context, provider) => Container(
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          image: DecorationImage(image: provider, fit: BoxFit.cover),
          border: borderWidth != null
              ? Border.all(color: borderColor!, width: borderWidth!)
              : null,
        ),
      ),
    );
  }

  PlaceholderWidgetBuilder? _placeholderBuilder() {
    if (placeholder != null) {
      return (context, url) => placeholder!;
    }
    return null;
  }

  LoadingErrorWidgetBuilder? _errorBuilder() {
    if (error != null) {
      return (context, url, error) => this.error!;
    }
    return null;
  }
}
